package org.xhy.sniffer.trace;


import java.util.Date;

public abstract class AbstractSpan {

    // 当前 spanId
    private int spanId;

    private CommentDefine comment;

    // traceId
    private String traceId;

    // 父 spanId
    private int parentSpanId;

    // 操作
    private String operationName;

    // 开始时间
    private Date startTime;

    // 异常信息
    private String throwableLog;

    // 地址
    private String peer;

    protected AbstractSpan(String operationName,CommentDefine comment){
        this.operationName = operationName;
        this.startTime = new Date();
        this.comment = comment;
    }

    protected AbstractSpan(String operationName,CommentDefine comment,String peer){
        this.operationName = operationName;
        this.startTime = new Date();
        this.comment = comment;
        this.peer = peer;
    }

    public void setThrowableLog(Throwable t){
        this.throwableLog = t.getMessage();
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    public int getSpanId(){
        return this.spanId;
    }

    public void setParentSpanId(int parentSpanId) {
        this.parentSpanId = parentSpanId;
    }

    public void setSpanId(int spanId) {
        this.spanId = spanId;
    }

    public String getOperationName() {
        return operationName;
    }

    public int getParentSpanId() {
        return parentSpanId;
    }

    public String getTraceId() {
        return traceId;
    }

    public Date getStartTime() {
        return startTime;
    }

    public String getPeer() {
        return peer;
    }

    public String getThrowableLog() {
        return throwableLog;
    }

    public CommentDefine getComment() {
        return comment;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("AbstractSpan {")
                .append("\n      Span ID: ").append(spanId)
                .append("\n      Trace ID: ").append(traceId)
                .append("\n      Parent Span ID: ").append(parentSpanId)
                .append("\n      Operation Name: ").append(operationName)
                .append("\n      Start Time: ").append(startTime)
                .append("\n      Throwable Log: ").append(throwableLog)
                .append("\n      Comment: ").append(comment != null ? comment.toString() : "No comment")
                .append("\n      Peer: ").append(peer)
                .append("\n    }");
        return sb.toString();
    }
}
